var Login = function() {

    var initIcheck = function () {
        $('input').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-blue',
            increaseArea: '20%' // optional
        });
    };

    var submitForm = function () {
        var username = $('#username').val();
        var password = $('#password').val();
        //var verify_code = $('#verify_code').val();
        var reg = /^\s*$/g;

        if(reg.test(username)){
            //click_alert_modal('请输入登录名！');
            layer.msg('请输入登录名！');
            $('#username').focus();
            return false;
        }

        if(reg.test(password)){
            //click_alert_modal('密码不能为空！');
            layer.msg('密码不能为空！');
            $('#password').focus();
            return false;
        }
        /*
        if(reg.test(verify_code)){
            click_alert_modal('验证码不能为空！');
            get_verify('#verify_code_img');
            return false;
        }
        */
        if($('#rememered').is(':checked')){
            //$.cookie('username', username, {expires:30});
        }

        var url = $('.login-form').attr('action');
        var data = {
            'username' : username,
            'password' : password
        };
        $.post(url, data, function(data){
            if(data.code == 1){
                window.location.href = APP + 'admin/dashboard/index';
            }else{
                //click_alert_modal(data.msg);
                layer.msg(data.msg);
            }
        });
        /*
        $.ajax({
            type : 'post',
            url : url,
            data : data,
            dataType : 'json',
            success : function(res){
                console.log(res);return false;
            }
        });
        return false;
        */
    };

    var keyDownSubmit = function (e) {
        // 兼容FF和IE和Opera
        var theEvent = e || window.event;
        var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
        if (code == 13) {
            submitForm();
            return false;
        }
        return true;
    };

    var handleLogin = function() {

        document.onkeydown = keyDownSubmit;

        /**
         * 登录表单提交验证
         */
        $('.login-btn').on('click', function(){
            submitForm();
        });
    };

    /**
     * alert模态框
     */
    var click_alert_modal = function(title){
        console.log(title);
        $('#alert_title').empty().html(title);
        $('#alert_modal').click();
    };

    return {
        //main function to initiate the module
        init: function() {
            initIcheck();
            handleLogin();
        }

    };

}();

jQuery(document).ready(function() {
    Login.init();
});